/*
 * File:   secondForm.cpp
 * Author: lynn
 *
 * Created on 2010年11月1日, 下午9:47
 */

#include "secondForm.h"
#include<QTableView>
#include <qt4/QtGui/qwindowdefs.h>

//设置槽函数进行第一个窗体的回复，但不知哪里出错，最后使用重载窗体关闭事件
void secondForm::closedForm()
{
    this->firstForm->show();
}

secondForm::secondForm(IplImage *frame, NewForm *Form, QWidget *parent):QDialog(parent){
    setupUi(this);
    this->tvParas->setColumnCount(3);
    this->tvParas->setRowCount(5);
    QStringList headers;
    headers<<"Speed"<<"Time"<<"direction";
    this->tvParas->setHorizontalHeaderLabels(headers);
    this->tvParas->setItem(0,0,new QTableWidgetItem(QString("50km/h")));
    this->tvParas->setItem(0,1,new QTableWidgetItem(QString("2010-1-1 20:00")));
    this->tvParas->setItem(0,0,new QTableWidgetItem(QString("west")));

    this->firstForm = Form;
    this->count = 0;
    connect(this,SIGNAL(accepted()),this,SLOT(closedForm()));
    if (frame){
        this->tvParas->setGeometry(frame->width,0,311,261);
        this->lblLeft->setGeometry(0,frame->height,frame->width,17);
        this->lblRight->setGeometry(0,frame->height*2+17,frame->width,17);
        this->setFixedSize(frame->width+311,frame->height*2+34);
    }
    qImg = new QImage(QSize(frame->width,frame->height),
            QImage::Format_RGB888);
    iplImg = cvCreateImageHeader(cvSize(frame->width,frame->height),
            8,3);
    iplImg->imageData = (char*)qImg->bits();

    //timer = new QTimer(this);
    //timer->setInterval(30);
    //timer->setInterval(30);
    //connect(timer,SIGNAL(timeout()),this,SLOT(nextFrame()));
		//timer->start();


    //showIplImage(img);

	//this->resize(img->width,img->height);
}
void secondForm::nextFrame(IplImage *frame)
{
	if (frame)
	{

		if (frame->origin == IPL_ORIGIN_TL)
		{
			cvCopy(frame,iplImg,0);
		}
		else
		{
			cvFlip(frame,iplImg,0);
		}

		cvCvtColor(iplImg,iplImg,CV_BGR2RGB);

		this->update();

	}
}
void secondForm::closeEvent(QCloseEvent* e) 
{
    this->firstForm->show();
}
void secondForm::paintEvent(QPaintEvent* e)
{
    QPainter painter(this);
    int width = qImg->width();
    int height = qImg->height();
    painter.drawImage(QPoint(0,0),*qImg);
    painter.drawImage(QPoint(0,height+17),*qImg);

}
//显示图片
void secondForm::showIplImage( IplImage *img)
{
    if(count == 0){
        this->tvParas->setGeometry(img->width,0,311,261);
        this->lblLeft->setGeometry(0,img->height,img->width,17);
        this->lblRight->setGeometry(0,img->height*2+17,img->width,17);
        this->setFixedSize(img->width+311,img->height*2+34);
        count = 1;
        qImg = new QImage(QSize(img->width,img->height),
 	QImage::Format_RGB888);
        iplImg = cvCreateImageHeader(cvSize(img->width,img->height),
        8,3);
        iplImg->imageData = (char*)qImg->bits();
    }
	if (img->origin == IPL_ORIGIN_TL)
	{
		cvCopy(img,iplImg,0);
	}
	else
	{
		cvFlip(img,iplImg,0);
	}
	cvCvtColor(iplImg,iplImg,CV_BGR2RGB);
        //this->update();
}

secondForm::~secondForm() {
    	cvReleaseImage(&iplImg);
	delete qImg;
}
